package com.HMI;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Image;
import javax.swing.ImageIcon;
import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;

public class Window {

	public JFrame frmAutomatedPoolBalancer;
	private MainMenu mainMenu;
	private MainWindow mainWindow;
	private ChlorineWindow chlorineWindow;
	private PHWindow phWindow;
	private ScheduleWindow scheduleWindow;
	private PopupWindow popupWindow;

	public final String MAIN = "Main";
	public final String CHLORINE = "Chlorine";
	public final String PH = "pH";
	public final String SCHEDULE = "Schedule";
	
	public Window() {
		initialize();
	}

	private void initialize() {
		initializeFrame();
		initializePopup();
		initializeMainMenu();
		initializeMainWindow();
		initializeChlorineWindow();
		initializePHWindow();
		initializeScheduleWindow();
		setBackgroundImage();
		setWindow(MAIN);
	}

	private void initializePopup() {
		popupWindow = new PopupWindow(this);
		frmAutomatedPoolBalancer.getContentPane().add(popupWindow);
		popupWindow.setFocusable(true);
		popupWindow.setVisible(false);
		popupWindow.setLayout(null);
	}
	
	private void initializeMainWindow() {
		mainWindow = new MainWindow(218, 0, 582, 480);
		frmAutomatedPoolBalancer.getContentPane().add(mainWindow);
		mainWindow.setLayout(null);
	}
	
	private void initializeChlorineWindow() {
		chlorineWindow = new ChlorineWindow(218, 0, 582, 480);
		frmAutomatedPoolBalancer.getContentPane().add(chlorineWindow);
		chlorineWindow.setLayout(null);
	}
	
	private void initializePHWindow() {
		phWindow = new PHWindow(218, 0, 582, 480);
		frmAutomatedPoolBalancer.getContentPane().add(phWindow);
		phWindow.setLayout(null);
	}
	
	private void initializeScheduleWindow() {
		scheduleWindow = new ScheduleWindow(218, 0, 582, 480, this);
		frmAutomatedPoolBalancer.getContentPane().add(scheduleWindow);
		scheduleWindow.setLayout(null);
	}
	
	private void initializeFrame() {
		frmAutomatedPoolBalancer = new JFrame();
		frmAutomatedPoolBalancer.getContentPane().setBackground(
				new Color(255, 255, 255));
		frmAutomatedPoolBalancer.setTitle("Automated Pool Balancer");
		frmAutomatedPoolBalancer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmAutomatedPoolBalancer.getContentPane().setLayout(null);
		frmAutomatedPoolBalancer.setBounds(0, 0, 800, 480);
		// frmAutomatedPoolBalancer.setExtendedState(Frame.MAXIMIZED_BOTH);
		// Sets window size to fill screen
		frmAutomatedPoolBalancer.setUndecorated(true); // Removes window border
	}

	private void setBackgroundImage() {
		try {
			ClassLoader classLoader = Thread.currentThread()
					.getContextClassLoader();
			InputStream input = classLoader
					.getResourceAsStream("com/Resources/background_480x800.png");
			Image logo = ImageIO.read(input);			
			JLabel backgroundImageLabel = new JLabel(new ImageIcon(logo));
			backgroundImageLabel.setBounds(218, 0, 582, 480);
			frmAutomatedPoolBalancer.getContentPane().add(backgroundImageLabel);	
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void initializeMainMenu() {
		mainMenu = new MainMenu(this);
	}
	
	public void setChlorineLevel(int level) {
		mainWindow.setChlorineLevel(level);
	}
	
	public void setWindow(String window) {
		switch (window) {
         case MAIN:
        	 scheduleWindow.setVisible(false);
        	 phWindow.setVisible(false);
        	 chlorineWindow.setVisible(false);
        	 mainWindow.setVisible(true);
             break;
         case CHLORINE:
        	 scheduleWindow.setVisible(false);
        	 mainWindow.setVisible(false);
        	 phWindow.setVisible(false);
        	 chlorineWindow.setVisible(true);
             break;
         case PH:
        	 scheduleWindow.setVisible(false);
        	 phWindow.setVisible(true);
        	 chlorineWindow.setVisible(false);
        	 mainWindow.setVisible(false);
             break;
         case SCHEDULE:
        	 phWindow.setVisible(false);
        	 chlorineWindow.setVisible(false);
        	 mainWindow.setVisible(false);
        	 scheduleWindow.setVisible(true);
             break;
     }
	}
	
	public void showPopup(String weekday, String month, String date, String hour, String minute, String ampm, Calendar calendar, boolean shockScheduled) {
		popupWindow.setInitialShockValues(weekday, month, date, hour, minute, ampm, calendar, shockScheduled);
		popupWindow.setVisible(true);
	}
	
	public void closePopop() {
		popupWindow.setVisible(false);
	}
	
	public void refreshPopup() {
		popupWindow.refresh();
	}
	
	public void updateScheduleDates() {
		scheduleWindow.updateDates();
	}
	
	public void addShockEvent(Calendar calendar) {
		scheduleWindow.addShockEvent(calendar);
	}
	
	public void removeShockEvent(Calendar calendar) {
		scheduleWindow.removeShockEvent(calendar);
	}
	
	public void updateGraphAxes() {
		chlorineWindow.updateXaxis();
		phWindow.updateXaxis();
	}
}
